package day3;

import java.util.Scanner;

import org.junit.Test;

public class ArrayDemo {
	@Test
	public void test() {
		int num = 3;
		int[] array = new int[4];
		array[0] = 23;
		array[1] = 3;
		array[2] = 4;
		array[3] = 24;// array[3]=array[array.length-1]
		System.out.println(array.length);// array.length数组长度
		for (int i = 0; i < array.length; i++) {
			System.out.println(array[i]);
		}
		System.out.println("----------------------");
		for (int i = array.length - 1; i >= 0; i--) {
			System.out.println(array[i]);
		}
	}
	
	@Test
	public void test2() {
		double[] array1 = new double[3];
		char[] array2 = new char[3];
		boolean[] array3 = new boolean[3]; 
	}
	
	@Test
	public void test3() {
//		int[] array = {23, 4, 5};
//		int[] array = new int[] {23, 4, 5};
		int[] array = new int[4];
		array[0] = 23;
		array[1] = 3;
		array[2] = 4;
		array[3] = 24;
		int max = array[0];
		int min = array[0];
		for (int i = 0; i < array.length; i++) {
			if (array[i] > max) {
				max = array[i];
			}
			if (array[i] < min) {
				min = array[i];
			}
		}
		System.out.println("max:" + max);
		System.out.println("min:" + min);
	}
	
	@Test
	public void test4() {// java.lang.ArrayIndexOutOfBoundsException: 4
		int[] array = {23, 3, 4, 24};
		for (int i = 0; i <= array.length; i++) {
			System.out.println(array[i]);
		}
	}
	
	@Test
	public void test5() {
		int[] array = {40, 17, 21, 1};
		for (int i = 0; i < array.length - 1; i++) {
			if (array[i] > array[i + 1]) {
				int temp = array[i];
				array[i] = array[i + 1];
				array[i + 1] = temp;
			}
		}
		for (int i = 0; i < array.length; i++) {
			System.out.println(array[i]);
		}
	}
	
	@Test
	public void test6() {
		int[] array = {40, 17, 21, 1};
		// 外层循环控制比较的趟数
		for (int i = 1; i <= array.length - 1; i++) {
			// 遍历整个数组，把最大值冒到最右边
			for (int j = 0; j < array.length - i; j++) {
				if (array[j] > array[j + 1]) {
					int temp = array[j];
					array[j] = array[j + 1];
					array[j + 1] = temp;
				}
			}
		}
		for (int i = 0; i < array.length; i++) {
			System.out.println(array[i]);
		}
	}
	
	
	@Test
	public void test7() {
		int[] array = new int[4];
		array[0] = 23;
		array[1] = 3;
		array[2] = 4;
		array[3] = 24;
		int max = getMax(array);// 数组名+参数
		System.out.println(max);
	}
	//传递一个整数型的数组，返回数组的最大值
	
	public int getMax(int[] array) {
		int max = array[0];
		for (int i = 0; i < array.length; i++) {
			if (array[i] > max) {
				max = array[i];
			}
		}
		return max;
	}
	
	@Test
	public void test8() {
		Scanner scanner = new Scanner(System.in);
		String str = scanner.nextLine();
		System.out.println(str);
		char[] array = str.toCharArray();
		for (int i = 0; i < array.length; i++) {
			System.out.print(array[i] + " ");
		}
	}
}
